package com.huayi.order.utils;

import com.huayi.order.entity.ErpSalesOrderItemVo;
import com.huayi.order.enums.EnumErpOrderStatus;
import com.huayi.order.enums.EnumSalesOrderPayMethod;
import com.huayi.order.enums.EnumSalesOrderPayStatus;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.List;

public class ExportOrderListExcecl {
    /**
     * 生产excel
     *
     * @param excelFileName
     */
    public static HSSFWorkbook createExcel(String excelFileName, List<ErpSalesOrderItemVo> lists) {
        //创建Excel工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();

        //创建Excel工作表对象
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row = null;
        HSSFCell cell = null;
        //第一行为空
        row = sheet.createRow(0);

        cell = row.createCell(0);
        cell.setCellValue("订单ID");

        cell = row.createCell(1);
        cell.setCellValue("订单号");

        cell = row.createCell(2);
        cell.setCellValue("子订单金额");

        cell = row.createCell(3);
        cell.setCellValue("商品");

        cell = row.createCell(4);
        cell.setCellValue("数量");

        cell = row.createCell(5);
        cell.setCellValue("单价");

        cell = row.createCell(6);
        cell.setCellValue("下单日期");

        cell = row.createCell(7);
        cell.setCellValue("收件人");

        cell = row.createCell(8);
        cell.setCellValue("收件人手机号");

        cell = row.createCell(9);
        cell.setCellValue("收件人地址");

        cell = row.createCell(10);
        cell.setCellValue("物流公司");

        cell = row.createCell(11);
        cell.setCellValue("物流单号");

        cell = row.createCell(12);
        cell.setCellValue("支付状态");

        cell = row.createCell(13);
        cell.setCellValue("支付方式");

        cell = row.createCell(14);
        cell.setCellValue("销售类型");

        cell = row.createCell(15);
        cell.setCellValue("发货状态");

        cell = row.createCell(16);
        cell.setCellValue("发货时间");

        cell = row.createCell(17);
        cell.setCellValue("卖家备注");

        cell = row.createCell(18);
        cell.setCellValue("订单运费");
        cell = row.createCell(19);
        cell.setCellValue("业务员");

        cell = row.createCell(20);
        cell.setCellValue("订单状态");
        int currRowNum = 0;
        // 循环写入数据
        for (int i = 0; i < lists.size(); i++) {
            currRowNum++;

            //写入订单
            ErpSalesOrderItemVo itemVo = lists.get(i);

            //创建行
            row = sheet.createRow(currRowNum);

            //订单ID
            cell = row.createCell(0);
            cell.setCellValue(itemVo.getOrderId());

            //订单号
            cell = row.createCell(1);
            cell.setCellValue(itemVo.getOrderNum());

            DecimalFormat df1 = new DecimalFormat("0.00");
            //子订单金额
            cell = row.createCell(2);
            cell.setCellValue(df1.format(itemVo.getItemAmount()));

            //商品
            cell = row.createCell(3);
            cell.setCellValue(itemVo.getGoodsTitle());
            //数量
            cell = row.createCell(4);
            cell.setCellValue(itemVo.getQuantity());
            //商品单价
            cell = row.createCell(5);
            cell.setCellValue(df1.format(itemVo.getPrice()));

            //下单日期
            cell = row.createCell(6);
            cell.setCellValue(itemVo.getOrderDate());

            //收件人
            cell = row.createCell(7);
            cell.setCellValue(itemVo.getContactPerson());
            //收件人手机号
            cell = row.createCell(8);
            cell.setCellValue(itemVo.getContactMobile());
            //收件人手机号
            cell = row.createCell(9);
            cell.setCellValue(itemVo.getAddress());
            //物流公司
            cell = row.createCell(10);
            cell.setCellValue(itemVo.getLogisticsCompany());
            //物流单号
            cell = row.createCell(11);
            cell.setCellValue(itemVo.getLogisticsCode());
            //支付状态
            cell = row.createCell(12);
            cell.setCellValue(EnumSalesOrderPayStatus.getName(itemVo.getPayStatus()));

            //支付方式
            cell = row.createCell(13);
            cell.setCellValue(EnumSalesOrderPayMethod.getName(itemVo.getPayMethod()));

            //销售类型
            String saleType = "实售";
            saleType = itemVo.getSaleType() == 1 ? "实售" : "样品";
            cell = row.createCell(14);
            cell.setCellValue(saleType);

            //发货状态
            cell = row.createCell(15);
            String deliveredStatusStr = itemVo.getDeliveredStatus().intValue()  == 1?"已发货":"未发货";
            cell.setCellValue(deliveredStatusStr);

            //发货时间
            cell = row.createCell(16);
            cell.setCellValue(DateUtil.unixTimeStampToDate(itemVo.getDeliveredTime(), "yyyy-MM-dd HH:mm:ss"));

            //卖家备注
            cell = row.createCell(17);
            cell.setCellValue(itemVo.getSellerMemo());

            //订单运费
            cell = row.createCell(18);
            cell.setCellValue(df1.format(itemVo.getShippingFee()));
            //
            cell = row.createCell(19);
            cell.setCellValue(itemVo.getSalesMan());

            cell = row.createCell(20);
            cell.setCellValue(EnumErpOrderStatus.getName(itemVo.getStatus()));
        }
        return workbook;
    }
}
